<html>
<script src="/js/jquery-2.1.1.min.js" type="text/javascript"></script>
<script src="/js/ckeditor/ckeditor.js" type="text/javascript"></script>
<link href="/js/ckeditor/contents.css" type="text/css" rel="stylesheet">
<script src="/js/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<link href="/js/bootstrap/css/bootstrap.min.css" type="text/css" rel="stylesheet">
<script src="/js/bootstrap-treeview/dist/bootstrap-treeview.min.js" type="text/javascript"></script>
<link href="/js/bootstrap-treeview/dist/bootstrap-treeview.min.css" type="text/css" rel="stylesheet">
<script src="/js/bootStrap-addTabs/bootstrap.addtabs.min.js" type="text/javascript"></script>
<link href="/js/bootStrap-addTabs/bootstrap.addtabs.css" type="text/css" rel="stylesheet">
<script src="/js/toastr/toastr.min.js" type="text/javascript"></script>
<link href="/js/toastr/toastr.css" type="text/css" rel="stylesheet">
<script src="/js/bootstrap-table/dist/bootstrap-table.min.js" type="text/javascript"></script>
<link href="/js/bootstrap-table/dist/bootstrap-table.min.css" type="text/css" rel="stylesheet">
<script src="/js/bootstrap-table/dist/locale/bootstrap-table-zh-CN.min.js" type="text/javascript"></script>
<script src="/js/bootstrap-fileinput/js/fileinput.min.js" type="text/javascript"></script>
<link href="/js/bootstrap-fileinput/css/fileinput.min.css" type="text/css" rel="stylesheet">
<script src="/js/bootstrap-fileinput/js/locales/zh.js" type="text/javascript"></script>
<script src="/js/bootstrap-dialog/dist/js/bootstrap-dialog.min.js" type="text/javascript"></script>
<link href="/js/bootstrap-dialog/dist/css/bootstrap-dialog.min.css" type="text/css" rel="stylesheet">
<script src="/js/zTreev3/js/jquery.ztree.all.js" type="text/javascript"></script>
<link href="/js/zTreev3/css/zTreeStyle/zTreeStyle.css" type="text/css" rel="stylesheet">
<style>
    .container-fluid {
        margin-top: 20px;
    }
</style>
<head>
    <title>用户管理</title>
</head>
<body>
<div id="toolbar">
    <form class="form-inline" role="form">
        <button type="button" class="btn btn-primary" onclick="adds()">添加用户</button>
    </form>
</div>
<table id="table"></table>
<script type="text/javascript">

    //刷新页面
    function showbypname() {
        $('#table').bootstrapTable("refresh")
    }

    $('#table').bootstrapTable({
        url: '/AdminUser/show',
        method: 'GET',
        toolbar: "#toolbar",
        striped: true,                      //是否显示行间隔色
        cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
        pagination: true,                   //是否显示分页（*）
        sidePagination: "client",           //分页方式：client客户端分页，server服务端分页（*）
        pageNumber: 1,                      //初始化加载第一页，默认第一页,并记录
        pageSize: 5,                     //每页的记录行数（*）
        pageList: [5, 10, 15],        //可供选择的每页的行数（*）
        search: true,                      //是否显示表格搜索
        showColumns: true,                  //是否显示所有的列（选择显示的列）
        showRefresh: true,                  //是否显示刷新按钮
        minimumCountColumns: 1,             //最少允许的列数
        clickToSelect: true,                //是否启用点击选中行
        height: 600,                      //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
        uniqueId: "userId",                     //每一行的唯一标识，一般为主键列
        showToggle: true,                   //是否显示详细视图和列表视图的切换按钮
        columns: [{
            field: 'userId',
            title: '编号',
            sortable: true
        }, {
            field: 'userName',
            title: '用户名称'
        }, {
            field: 'userPwd',
            title: '用户密码'
        },  {
            field: 'birth',
            title: '用户生日'
        },{
            field: 'userSex',
            title: '用户性别',
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "男";
                }
                return "女";
            }
        }, {
            field: 'depId',
            title: '用户状态',
            formatter: function (value, row, index) {
                if (value < 3) {
                    return "正常";
                }
                if (value >= 3) {
                    return "冻结";
                }
            }
        }, {
            field: 'userId',
            title: '操作',
            width: 120,
            align: 'center',
            valign: 'middle',
            formatter: function actionFormatter(value, row, index) {
                var id = value;
                var result = "";
                result += "<a href='javascript:updates(" + id + ")' class='btn btn-xs blue' title='编辑'><span class='glyphicon glyphicon-pencil'></span></a>";
                result += "<a href='javascript:deletebyid(" + id + ")' class='btn btn-xs red'  title='删除'><span class='glyphicon glyphicon-remove'></span></a>";
                return result;
            }
        },]
    });
    $("[name=refresh]").attr("style", "height:34px;");
    $("[name=toggle]").attr("style", "height:34px;");
    $("[data-toggle=dropdown]").attr("style", "height:34px;");

    //实现添加数据的方法
    function adds(pid) {
        BootstrapDialog.show({
            title: '添加',
            message: function (dialog) {
                return $("<div></div>").load("/AdminUser/tzadd");
            },
            type: BootstrapDialog.TYPE_PRIMARY,
            size: BootstrapDialog.SIZE_NORMAL,
            closeable: true,
            buttons: [{
                label: '添加',
                cssClass: 'btn btn-primary',
                action: function (dialog) {
                    if ($("[name = userName]").val() == null || $("[name = userName]").val() == "") {
                        toastr.error("用户名称不能为空");
                        return;
                    }
                    if ($("[name = userPwd]").val() == null || $("[name = userPwd]").val() == "") {
                        toastr.error("用户密码不能为空");
                        return;
                    }
                    $.post("/AdminUser/add", $("#forms").serialize(), function (pm) {
                        if (pm == 1) {
                            toastr.success("添加成功")
                            dialog.close();
                            showbypname();
                            return
                        }
                        toastr.error("添加失败");
                    }, "text")
                }
            }, {
                label: '关闭',
                action: function (dialog) {
                    dialog.close();
                }
            }]
        });
    }

    //实现更新数据的方法
    function updates(userId) {
        BootstrapDialog.show({
            title: '修改',
            message: function (dialog) {
                return $("<div></div>").load("/AdminUser/tzupdate?userId=" + userId);
            },
            type: BootstrapDialog.TYPE_PRIMARY,
            size: BootstrapDialog.SIZE_NORMAL,
            closeable: true,
            buttons: [{
                label: '修改',
                cssClass: 'btn btn-primary',
                action: function (dialog) {
                    if ($("[name = userName]").val() == null || $("[name = userName]").val() == "") {
                        toastr.error("用户名称不能为空");
                        return;
                    }
                    if ($("[name = userPwd]").val() == null || $("[name = userPwd]").val() == "") {
                        toastr.error("用户密码不能为空");
                        return;
                    }
                    $.get("/AdminUser/update", $("#forms").serialize(), function (pm) {
                        if (pm == 1) {
                            toastr.info("修改成功")
                            dialog.close();
                            $('#table').bootstrapTable("refresh");
                        } else
                            toastr.error("修改失败");
                    }, "text")
                }
            }, {
                label: '关闭',
                action: function (dialog) {
                    dialog.close();
                }
            }]
        });
    }

    //根据id进行删除
    function deletebyid(userId) {
        if (confirm("你确定要删除吗")) {
            $.get("/AdminUser/delete", {userId: userId}, function (ppm) {
                if (ppm == 1) {
                    toastr.error("删除成功");
                    $('#table').bootstrapTable("refresh");
                    return;
                }
                toastr.error("删除失败");
            }, "text")
        }
    }
</script>
</body>
</html>